package interchange.data;

import interchange.domain.Objeto;
import interchange.utiles.UIDGenerator;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class JDBCObjetoDAO implements IObjetoDAO {

	public Objeto select(Connection conn, String OIDObjeto) {
		PreparedStatement stmt = null;
        ResultSet result = null;
        Objeto o = null;
        String sql = "SELECT * FROM Objeto WHERE (OID = ?) ";

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, OIDObjeto);
            result = stmt.executeQuery();

            result.next();
            o = new Objeto();
            o.setDescripcion(result.getString("descripcion"));
            o.setCategoria(result.getString("categoria"));
            o.setIDObjeto(result.getString("IDObjeto"));
            
        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (result != null) {
                    result.close();
                }
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
        return o;
}
	

	public List<Objeto> selectAllObjetos() {
		 Connection conn = ConnectionManager.getInstance().checkOut();

	      PreparedStatement stmt = null;
	        List<Objeto> searchResults = new LinkedList<Objeto>();
	        ResultSet result = null;

	        try {

	            String sql = "SELECT * FROM Objeto";
	            stmt = conn.prepareStatement(sql); //conn es una conexion valida
	            stmt.executeQuery();
	            result = stmt.executeQuery();

	            while (result.next()) {
	                Objeto temp = new Objeto();
	                temp.setIDObjeto(result.getString("IDObjeto"));
	                temp.setDescripcion(result.getString("descripcion"));
	                temp.setCategoria(result.getString("categoria"));
	                searchResults.add(temp);
	            }
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            ConnectionManager.getInstance().checkIn(conn);
	            try {
	                if (result != null)
	                    result.close();
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }

	        }
	        return searchResults;
		
	}

	public String selectOIDObjeto(Connection conn, String IDObjeto) {
		 PreparedStatement stmt = null;
	        ResultSet result = null;
	        String oido = null;
	        String sql = "SELECT * FROM Objeto WHERE (IDObjeto = ?) ";

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, IDObjeto);
	            result = stmt.executeQuery();

	            result.next();
	            oido = result.getString("OID");
	            
	        } catch (SQLException e) {
	            System.out.println("Message: " + e.getMessage());
	            System.out.println("SQLState: " + e.getSQLState());
	            System.out.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (result != null) {
	                    result.close();
	                }
	                if (stmt != null) {
	                    stmt.close();
	                }
	            } catch (SQLException e) {
	            }
	        }
	        return oido;
	    }
	
	public void delete(Connection conn, String IDObjeto){
		
		 String sql = "DELETE FROM Objeto WHERE (OIDObjeto = ?) ";
	        PreparedStatement stmt = null;

	        try {
	            stmt = conn.prepareStatement(sql);
	            stmt.setString(1, IDObjeto);
	            stmt.executeUpdate();
	        } catch (SQLException e) {
	            System.err.println("Message: " + e.getMessage());
	            System.err.println("SQLState: " + e.getSQLState());
	            System.err.println("ErrorCode: " + e.getErrorCode());
	        } finally {
	            try {
	                if (stmt != null)
	                    stmt.close();
	            } catch (SQLException e) {
	            }
	        }
		
	}


	public void insert(Connection conn, String OIDObjeto, Objeto o) {
		
		String oido = UIDGenerator.getInstance().getKey();

		String sql = "INSERT INTO Objeto ( OIDObjeto, idObjeto, descripcion, categoria, subastado) VALUES (?, ?, ?, ?, ?) ";
	
        PreparedStatement stmt = null;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, oido);
            stmt.setString(2, o.getObjetoID());
            stmt.setString(3, o.getDescripcion());
            stmt.setString(4, o.getCategoria());
            stmt.setBoolean(5, o.getSubastado());
            
            stmt.executeUpdate();

        } catch (SQLException e) {
            System.out.println("Message: " + e.getMessage());
            System.out.println("SQLState: " + e.getSQLState());
            System.out.println("ErrorCode: " + e.getErrorCode());
        } finally {
            try {
                if (stmt != null) {
                    stmt.close();
                }
            } catch (SQLException e) {
            }
        }
		
	}
	}

	
	


